package com.lp.service;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Arrays;

@Data
@AllArgsConstructor
public class Game {

    private String bUser;
    private String aUser;
    private String lastUser;
    private Integer count = 0;
    private int[][] oriData = new int[15][25];

    public Game() {
        for (int[] oriDatum : oriData) {
            Arrays.fill(oriDatum, -1);
        }
    }


    public int isSuccess(int x, int y, int f) {
        //y的范围在0-14之间，x的范围在0-24之间
        int count = 0;
        for (int i = x - 1; i > -1; i--) {
            if (oriData[y][i] != f) {
                break;
            }
            count++;
        }
        for (int i = x + 1; i < 25; i++) {
            if (oriData[y][i] != f) {
                break;
            }
            count++;
        }
        if (count > 3) {
            return f;
        }
        count = 0;
        for (int i = y + 1; i < 15; i++) {
            if (oriData[i][x] != f) {
                break;
            }
            count++;
        }
        for (int i = y - 1; i > -1; i--) {
            if (oriData[i][x] != f) {
                break;
            }
            count++;
        }
        if (count > 3) {
            return f;
        }
        count = 0;
        for (int i = x + 1, j = y + 1; i < 25; i++, j++) {
            if (j < 15) {
                if (oriData[j][i] != f) {
                    break;
                }
                count++;
            } else {
                break;
            }
        }
        for (int i = x - 1, j = y - 1; i > -1; i--, j--) {
            if (j > -1) {
                if (oriData[j][i] != f) {
                    break;
                }
                count++;
            } else {
                break;
            }
        }
        if (count > 3) {
            return f;
        }
        count = 0;
        for (int i = x + 1, j = y - 1; i < 25; i++, j--) {
            if (j > -1) {
                if (oriData[j][i] != f) {
                    break;
                }
                count++;
            } else {
                break;
            }
        }
        for (int i = x - 1, j = y + 1; i > -1; i--, j++) {
            if (j < 25) {
                if (oriData[j][i] != f) {
                    break;
                }
                count++;
            } else {
                break;
            }
        }
        if (count > 3) {
            return f;
        }
        return -1;
    }
}